/*
 *    Copyright (c) 2018-2025, fanxl All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: fanxl (xunlian_fan001@126.com)
 */
package com.taiji.dicom4cloud.smart.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.IPage;
import com.github.pagehelper.util.StringUtil;
import com.taiji.dicom4cloud.common.core.utils.R;
import com.taiji.dicom4cloud.smart.controller.AlarmRecordController;
import com.taiji.dicom4cloud.smart.dao.AlarmRecordMapper;
import com.taiji.dicom4cloud.smart.model.entity.Camera;
import com.taiji.dicom4cloud.smart.model.entity.FtpFile;
import com.taiji.dicom4cloud.smart.model.entity.RfidRecord;
import com.taiji.dicom4cloud.smart.service.AlarmRecordService;
import com.taiji.dicom4cloud.smart.service.CameraEquipmentService;
import com.taiji.dicom4cloud.smart.service.FtpFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author fanxl
 * @date 2021-07-06 10:46:51
 */
@Service
public class AlarmRecordServiceImpl extends ServiceImpl<AlarmRecordMapper, RfidRecord> implements AlarmRecordService {

    private static final Logger LOG = LoggerFactory.getLogger(AlarmRecordController.class);



    @Autowired
    private CameraEquipmentService cameraEquipmentService;

    @Autowired
    private FtpFileService ftpFileService;

    @Override
    public Boolean saveRecord(RfidRecord record) {
        return this.save(record);
    }


    @Override
    public Page getAlarmRecords(Integer current,Integer total, String orgId,String type) {
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(10);
        page.setTotal(total);

        Page rfidRecords = this.page(page,Wrappers.<RfidRecord>lambdaQuery()
                .eq(StringUtil.isNotEmpty(orgId)&&!orgId.equals("-1"),RfidRecord::getOrgId,orgId)
                .eq(!type.equals("-1"),RfidRecord::getType,type)
                .eq(RfidRecord::getFlag,"0")
                .orderByDesc(RfidRecord::getId));

        List<RfidRecord> rfidRecords1 = rfidRecords.getRecords();
        return rfidRecords;
    }

    @Override
    public Page getAlarmRecordPage(Page page, RfidRecord rfidRecord) {
        Page rfidRecords = this.page(page, Wrappers.<RfidRecord>lambdaQuery()
                .eq(StringUtil.isNotEmpty(rfidRecord.getOrgId())&&!rfidRecord.getOrgId().equals("-1"), RfidRecord::getOrgId,rfidRecord.getOrgId())
                .eq(StringUtil.isNotEmpty(rfidRecord.getIsAlarm()),RfidRecord::getIsAlarm,rfidRecord.getIsAlarm())
                .eq(StringUtil.isNotEmpty(rfidRecord.getType())&&!rfidRecord.getType().equals("-1"),RfidRecord::getType,rfidRecord.getType())
                .like(StringUtil.isNotEmpty(rfidRecord.getEquipmentName()),RfidRecord::getEquipmentName,rfidRecord.getEquipmentName())
                .like(StringUtil.isNotEmpty(rfidRecord.getOwnerName()),RfidRecord::getOwnerName,rfidRecord.getOwnerName())
                .orderByDesc(RfidRecord::getCreateTime));
        return rfidRecords;
    }

    @Override
    public RfidRecord saveCameraData(JSONObject msg) {
        JSONObject jsonObject = msg;
        String msgType = jsonObject.getString("msgType");
        String messageId = jsonObject.getString("messageId");
        String time = jsonObject.getString("time");
        String deviceId = jsonObject.getString("deviceId");
        RfidRecord record = new RfidRecord();

        Camera camera = cameraEquipmentService.getOne(Wrappers.<Camera>lambdaQuery().eq(Camera::getDeviceId,deviceId));
        if(ObjectUtil.isNotNull(camera)){
            record.setOrgName(camera.getOrgName());
            record.setOrgId(camera.getOrgId());
            record.setLatitude(camera.getLatitude());
            record.setLongitude(camera.getLongitude());
            record.setEquipmentName(camera.getCameraEquipmentName());
            record.setEquipmentId(deviceId);

        }
        record.setAlarmTime(LocalDateTime.now());
        record.setAlarmName(msgType);
        record.setCameraMessageId(messageId);
        record.setType("2");
        record.setCreateTime(LocalDateTime.now());
        this.save(record);
        LOG.info("大华预警回调入库成功");
        return record;
    }

    @Override
    public List<String> getVideoUrl(Map<String, String> map) {
        List<String> list = new ArrayList<>();
        List<FtpFile> ftpFiles = ftpFileService.listFtpFile(map.get("time"),map.get("deviceId"));
        if(ObjectUtil.isNotNull(ftpFiles)){
            for(FtpFile ftpFile:ftpFiles){
                if(ObjectUtil.isNotNull(ftpFile.getTranPath())){
                    list.add(ftpFile.getTranPath());
                }
            }
        }
        return list;
    }
}